<?php

namespace App\Utils;

use p5\objects\PropertyUtils;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Support\Facades\Log;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/8/12
 * Time: 9:36
 */
class ExcelExportUtils
{
    private $path = "";
    private $columns = [];
    private $data = [];
    private $file = null;
    private $titles = [];

    public function saveToFile()
    {
        Excel::create($this->file, function ($excel) {
            $excel->sheet('sheet1', function ($sheet) {
                $sheet->fromArray($this->data, null, 'A1', true, false);
            });
        })->store('xls', $this->path);
    }

    public function addLine($line)
    {
        array_push($this->data, $line);
    }

    public function addDataIntoLine($lineIndex, $data)
    {
        foreach ($data as $item) {
            array_push($this->data[$lineIndex], $item);
        }
    }

    public function addLinesWithHash($lines)
    {
        foreach ($lines as $line) {
            $this->addLineWithHash($line);
        }
    }

    public function addLineWithHash($hash)
    {
        $values = [];
        foreach ($this->columns as $column) {
            $value = PropertyUtils::getProperty($hash, $column);
            array_push($values, $value);
        }
        array_push($this->data, $values);
    }

    public function addTitleLine()
    {
        $values = [];
        foreach ($this->columns as $column) {
            $matrix = explode("-", $column);
            $name = $matrix[0];
            $value = PropertyUtils::getProperty($this->titles, $name, $name);
            if (count($matrix) > 1) {
                $value = $value . "-" . $matrix[1];
            }
            array_push($values, $value);
        }
        array_push($this->data, $values);
    }

    /**
     * @return array
     */
    public function getColumns()
    {
        return $this->columns;
    }

    /**
     * @param array $columns
     */
    public function setColumns($columns)
    {
        $this->columns = $columns;
    }

    public function addColumn($name)
    {
        if (in_array($name, $this->columns)) {
            return;
        }
        array_push($this->columns, $name);
    }

    public function getExportCount()
    {
        return count($this->data);
    }

    /**
     * @return array
     */
    public function getData()
    {
        return $this->data;
    }

    /**
     * @param array $data
     */
    public function setData($data)
    {
        $this->data = $data;
    }

    public function addData($matrix)
    {
        array_push($this->data, $matrix);
    }

    /**
     * @return array
     */
    public function getTitles()
    {
        return $this->titles;
    }

    /**
     * @param array $titles
     */
    public function setTitles($titles)
    {
        $this->titles = $titles;
    }


    /**
     * @return null
     */
    public function getFile()
    {
        return $this->file;
    }

    /**
     * @param null $file
     */
    public function setFile($file)
    {
        if (empty($file)) {
            return;
        }
        $this->setPath(dirname($file));
        $basename = basename($file);
        $ext = strrchr($basename, ".");
        if (in_array($ext, [".xls", ".csv"])) {
            $basename = str_replace($ext, "", $basename);
        }
        // $this->setFile($basename);
        Log::debug("set real file to $basename");
        $this->file = $basename;
    }

    private function initColumns()
    {
        if (!empty($this->columns)) {
            return;
        }
        if (empty($this->data)) {
            return;
        }
        $this->columns = array_keys($this->data[0]);
    }

    /**
     * @return string
     */
    public function getPath()
    {
        return $this->path;
    }

    /**
     * @param string $path
     */
    public function setPath($path)
    {
        $this->path = $path;
    }


    public function clear()
    {
        $this->columns = [];
        $this->data = [];
    }

}